package org.study.jetcache.boot.common;

import jakarta.annotation.PostConstruct;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;

import java.util.*;
import java.util.concurrent.CompletableFuture;

/**
 * 统一缓存对象行为：支持缓存预加载
 *
 * @author Administrator
 */
public abstract class AbstractCacheOperateWarmUp<K, V> implements ApplicationRunner, Ordered {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 异步加载
        CompletableFuture.runAsync(this::preLoadCache);
    }

    @Override
    public int getOrder() {
        return Integer.MIN_VALUE + 1;
    }

    /**
     * bean 初始化后调用
     */
    @PostConstruct
    protected abstract void refreshLoad();

    /**
     * 缓存预热
     */
    protected abstract void preLoadCache();

    /**
     * 新增缓存
     *
     * @param key
     * @param value
     */
    public abstract void addCache(K key, V value);

    /**
     * 批量新增缓存
     *
     * @param map
     */
    public abstract void addCacheBatch(Map<K, V> map);

    /**
     * 批量缓存
     *
     * @param keys
     */
    public abstract void deleteCacheBatch(Set<K> keys);

    /**
     * 获取单个缓存
     *
     * @param key
     * @return
     */
    public abstract V getCacheValue(K key);

    /**
     * 批量获取缓存list
     *
     * @param keys
     * @return
     */
    public abstract List<V> getBatch4List(Set<K> keys);

    /**
     * 批量获取缓存map
     *
     * @param keys
     * @return
     */
    public abstract Map<K, V> getBatch4Map(Set<K> keys);
}
